package cn.xinfei.xdecision.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.stereotype.Service;

/**
 * 多事件监听
 */
@Slf4j
@Service
public abstract class XMultApplicationEventListener implements SmartApplicationListener {

    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> aClass) {
        for (Class cls : listenerEvent()) {
            if (cls.isAssignableFrom(aClass)) {
                return true;
            }
        }
        return false;
    }

    protected abstract Class<?>[] listenerEvent();

    protected abstract void onXApplicationEvent(ApplicationEvent applicationEvent);

    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        onXApplicationEvent(applicationEvent);
    }
}
